﻿using System;
using System.Threading;

namespace Net.Share
{
    /// <summary>
    /// 远程过程调用异步模型 async和await实现和委托事件处理
    /// </summary>
    public class RpcModelAsync
    {
        /// <summary>
        /// 是否完成操作
        /// </summary>
        public bool IsCompleted { get; internal set; }
        /// <summary>
        /// 响应的数据在这里
        /// </summary>
        public IRpcModel model;
        /// <summary>
        /// 立即返回, 不执行后面的[Rpc]标签的方法
        /// </summary>
        public bool Intercept { get; set; }
        internal Action callback;

        public override string ToString()
        {
            return $"IsCompleted:{IsCompleted} intercept:{Intercept} model:{model}";
        }

        public void SetCompleted(IRpcModel model)
        {
            this.model = model;
            IsCompleted = true;
            var original = Interlocked.Exchange(ref callback, null);
            original?.Invoke();
        }
    }
}
